<template>
  <div class="login-container">
    <div class="LoginBox">
      <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form" label-position="left">
        <div class="title-container">
          <h3 class="title">大数据管理系统</h3>
        </div>

        <el-form-item prop="username">
          <!-- <span class="svg-container">
              <svg-icon icon-class="user" />
            </span> -->
          <el-input ref="username" v-model="loginForm.username" placeholder="请输入账号" name="username" type="text"
            tabindex="1" prefix-icon="el-icon-user"></el-input>
        </el-form-item>

        <el-form-item prop="password">
          <!-- <span class="svg-container">
              <svg-icon icon-class="password" />
            </span> -->
          <el-input :key="passwordType" ref="password" v-model="loginForm.password" :type="passwordType"
            placeholder="请输入密码" prefix-icon="el-icon-lock" name="password" tabindex="2"
            @keyup.enter.native="handleLogin" show-password></el-input>
          <!-- <span class="show-pwd" @click="showPwd">
              <svg-icon :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'" />
            </span> -->
        </el-form-item>
        <el-form-item prop="password">
          <div class="DenifyDivBox">
            <div>
              <el-input type="text" v-model="DenfityCode" prefix-icon="el-icon-chat-dot-square" auto-complete="off"
                placeholder="验证码"></el-input>
            </div>
            <div @click="refreshCode" class="RidentifyBox">
              <Denfity :identifyCode="identifyCode"></Denfity>
            </div>
          </div>
        </el-form-item>
        <el-button :loading="loading" type="primary" style="width: 100%; margin-bottom: 30px"
          @click.native.prevent="handleLogin">登录</el-button>

        <!-- <div class="tips">
            <span style="margin-right: 20px">username: admin</span>
            <span> password: any</span>
          </div> -->
      </el-form>
    </div>
  </div>
</template>

<script>
  import {
    validUsername
  } from "@/utils/validate";
  import Denfity from '../../components/dentify.vue'
  export default {
    name: "Login",
    components: {
      Denfity,
    },
    data() {
      const validateUsername = (rule, value, callback) => {
        callback();
        // if (!validUsername(value)) {
        //   callback(new Error("Please enter the correct user name"));
        // } else {
        //   callback();
        // }
      };
      const validatePassword = (rule, value, callback) => {
        callback();
        // if (value.length < 6) {
        //   callback(new Error("The password can not be less than 6 digits"));
        // } else {
        //   callback();
        // }
      };
      return {
        DenfityCode: '',
        identifyCode: '',
        identifyCodes: "23456789abcdefghjkmnpqrstuvwxyz",
        loginForm: {
          username: "",
          password: "",
        },
        loginRules: {
          username: [{
            required: true,
            trigger: "blur",
            validator: validateUsername
          }, ],
          password: [{
            required: true,
            trigger: "blur",
            validator: validatePassword
          }, ],
        },
        loading: false,
        passwordType: "password",
        redirect: undefined,
      };
    },
    watch: {
      $route: {
        handler: function(route) {
          this.redirect = route.query && route.query.redirect;
        },
        immediate: true,
      },
    },
    mounted() {
      // 初始化验证码
      this.identifyCode = "";
      //参数：（1）随机串内容。（2）验证码显示位数
      this.makeCode(this.identifyCodes, 4);
    },
    methods: {
      // showPwd() {
      //   if (this.passwordType === "password") {
      //     this.passwordType = "";
      //   } else {
      //     this.passwordType = "password";
      //   }
      //   this.$nextTick(() => {
      //     this.$refs.password.focus();
      //   });
      // },
      refreshCode() {
        this.identifyCode = "";
        this.makeCode(this.identifyCodes, 4);
      },
      makeCode(o, l) {
        for (let i = 0; i < l; i++) {
          //通过循环获取字符串内随机几位
          this.identifyCode +=
            this.identifyCodes[this.randomNum(0, this.identifyCodes.length)];
        }
        this.DenfityCode = ''
      },
      //随机数字：用于当角标拿字符串的值
      randomNum(min, max) {
        return Math.floor(Math.random() * (max - min) + min);
      },
      handleLogin() {
        // this.$router.push({ path: '/templates' });
        // console.log("1111");
        this.$refs.loginForm.validate((valid) => {
          if (valid) {
            // if(this.identifyCode==this.DenfityCode){
              this.loading = true;
              this.$router.push({ path: '/' });
            // this.$store
            //   .dispatch("user/login", this.loginForm)
            //   .then(() => {
            //     this.$router.push({
            //       path: this.redirect || "/"
            //     });
            //     localStorage.setItem('CsvUsername', this.loginForm.username);
            //     this.loading = false;
            //   })
            //   .catch(() => {
            //     this.loading = false;
            //   });
            // }else{
            //   this.$message({
            //     message: '验证码错误',
            //     type: 'error',
            //     duration: 1000
            //   });
            // }
          } else {
            console.log("error submit!!");
            return false;
          }
        });
      },
    },
  };
</script>



<style lang="scss" scoped>
  $bg: #2d3a4b;
  $dark_gray: #889aa4;
  $light_gray: #eee;

  .login-container {
    min-height: 100%;
    width: 100%;
    // background-color: $bg;
    background-image: url('../../assets/BackG.png');
    overflow: hidden;
    display: flex;
    align-items: center;
    justify-content: flex-end;
       .LoginBox {
         border-radius: 10px;
         margin:0 12% 0 0;
         width: 380px;
         height: 400px;
         padding: 30px 35px 15px 35px;
         background: #fff;
         border: 1px solid #eaeaea;
         text-align: left;
         box-shadow: 0 0 20px 2px rgba(0, 0, 0, 0.1);
         // position: absolute;
         // top: 30%;
         // right: 12%;
       }




    .tips {
      font-size: 14px;
      color: #fff;
      margin-bottom: 10px;

      span {
        &:first-of-type {
          margin-right: 16px;
        }
      }
    }

    .svg-container {
      padding: 6px 5px 6px 15px;
      color: $dark_gray;
      vertical-align: middle;
      width: 30px;
      display: inline-block;
    }

    .title-container {
      position: relative;

      .title {
        font-size: 26px;
        color: #000;
        margin: 0px auto 40px auto;
        text-align: center;
        font-weight: bold;
      }
    }

    .show-pwd {
      position: absolute;
      right: 10px;
      top: 7px;
      font-size: 16px;
      color: $dark_gray;
      cursor: pointer;
      user-select: none;
    }
  }

  .DenifyDivBox {
    display: flex;
    align-items: center;
    justify-content: space-between;
  }

  .RidentifyBox {
    height: 40px !important;
    display: flex;
    height: auto;
  }

  ::v-deep .el-input__icon {
    font-size: 20px;
    color: #70BBFF;
  }

  .el-input ::v-deep .el-input__inner {
    border: 1px solid #70BBFF;
  }
</style>
